package com.everflourish.yeah100.utils;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

/**
 * toast封装工具类
 *
 * @author pf
 * @date 2014-11-19
 */
public class MyToast {

    private static final String DATA_ERROR = "数据发生异常";

    static Toast t;
    static int mGravity = Gravity.CENTER;

    /**
     * 显示toast(long)
     *
     * @param context
     * @param str
     */
    public static void showLong(Context context, String str) {
        if (t != null) {
            t.cancel();
        }
        t = Toast.makeText(context, str, Toast.LENGTH_LONG);
        show();
    }

    /**
     * 显示toast(long)
     *
     * @param context
     * @param resId
     */
    public static void showLong(Context context, int resId) {
        if (t != null) {
            t.cancel();
        }
        t = Toast.makeText(context, resId, Toast.LENGTH_LONG);
        show();
    }

    /**
     * 显示toast(short)
     *
     * @param context
     * @param resId
     */
    public static void showShort(Context context, int resId) {
        if (t != null) {
            t.cancel();
        }
        t = Toast.makeText(context, resId, Toast.LENGTH_SHORT);
        show();
    }

    /**
     * 显示toast(short)
     * @param context
     * @param str
     */
    public static void showShort(Context context, String str) {
        if (t != null) {
            t.cancel();
        }
        t = Toast.makeText(context, str, Toast.LENGTH_SHORT);
        show();
    }

    public static void showDataError(Context context) {
        if (t != null) {
            t.cancel();
        }
        t = Toast.makeText(context, DATA_ERROR, Toast.LENGTH_SHORT);
        show();
    }

    private static void show() {
        t.setGravity(mGravity, 0, 0);
        t.show();
    }
}
